package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.mapper.PomrDao;
import com.utils.ObjectUtils;
import com.utils.report.ExcelTsPrint;
import com.utils.report.ExcelUtil;


@Service(value="pomrsDownService")
public class PomrsDownService extends ExcelTsPrint{
	
	@Autowired
	private PomrDao pomrDao;
	
	private String ocode;
	
	@Override
	public void tsPrint(Workbook hssfWorkbook2,Sheet sheet2) {
		try {
			
			//print
			Sheet sheet = null;
			try{
				sheet = hssfWorkbook2.createSheet("居民健康档案");
			}catch (Exception e) {
				sheet = hssfWorkbook2.createSheet("居民健康档案");
				e.printStackTrace();
			}
			//设置一页高 一页宽
			HSSFPrintSetup psss = (HSSFPrintSetup) sheet.getPrintSetup();
			sheet.setAutobreaks(true);
			psss.setFitHeight((short)1);
			psss.setFitWidth((short)1);
			
			//复制第一个模板sheet
			CopySheet(sheet,sheet2);
			
			ArrayList<LinkedHashMap<String,Object>> listdata = null;
			try {
				listdata = getPomrsInfo(ocode);
			} catch (Exception e) {
				e.printStackTrace();
			}
			/*打印信息*/
			for(int i=0; i<listdata.size(); i++){
				Map<String,Object> map = listdata.get(i);
				
				int j=0;
				for(String key:map.keySet()){
					ExcelUtil.poiSetCellVal(sheet, i+1 , j, ObjectUtils.getStrVal(map.get(key)));
					j++;
				}
			}
			
		//	sheet.protectSheet(MD5.encodeByMD5(String.valueOf(new Random().nextInt(10000000))));//表格设置密码
			
			hssfWorkbook2.removeSheetAt(0);//打印完成 移除第一个模版sheet
			//hssfWorkbook.removeSheetAt(1);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	ArrayList<LinkedHashMap<String,Object>> getPomrsInfo(String ocode) throws Exception{
		
		ArrayList<LinkedHashMap<String,Object>> listdata = new ArrayList<LinkedHashMap<String,Object>>();
		
		ArrayList<HashMap<String,Object>> listPomr = new ArrayList<HashMap<String,Object>>(); 
		listPomr = pomrDao.getBasePomrs(ocode);
		for (int i=0; i<listPomr.size();i++) {
			
			LinkedHashMap<String, Object> mapTmp = new LinkedHashMap<String, Object>(); 
			
			HashMap<String,Object> map = listPomr.get(i);
			
			//add baseinfonations
			mapTmp.put("jkdah", map.get("jkdah"));
			mapTmp.put("jdjg", map.get("jdjg"));
			mapTmp.put("jdr", map.get("jdr"));
			mapTmp.put("xm", map.get("xm"));
			mapTmp.put("xbm", map.get("xmb"));
			mapTmp.put("jdsj", map.get("jdsj"));
			mapTmp.put("zjlx", map.get("zjlx"));
			mapTmp.put("zjh", map.get("zjh"));
			mapTmp.put("jdah", map.get("jdah"));
			mapTmp.put("hyzk", map.get("hyzk"));
			mapTmp.put("zw", map.get("zw"));
			mapTmp.put("whcd", map.get("whcd"));
			mapTmp.put("brdh", map.get("brdh"));
			mapTmp.put("lxrdh", map.get("lxrdh"));
			mapTmp.put("lxrxm", map.get("lxrxm"));
			mapTmp.put("fqxm", map.get("fqxm"));
			mapTmp.put("mqxm", map.get("mqxm"));
			mapTmp.put("jhrxm", map.get("jhrxm"));
			mapTmp.put("jtdh", map.get("jtdh"));
			mapTmp.put("rqlx", map.get("rqlx"));
			mapTmp.put("jzlx", map.get("jzlx"));
			mapTmp.put("xzzqwh", map.get("xzzqwh"));
			mapTmp.put("hjdwyh", map.get("hjdwyh"));
			mapTmp.put("xzz", map.get("xzz"));
			mapTmp.put("hjzz", map.get("hjzz"));
			mapTmp.put("rh", map.get("rh"));
			mapTmp.put("xx", map.get("xx"));
			
			
			int uid = ObjectUtils.getIntVal(map.get("uid").toString());
			
			//ywgms
			String strYwgms = "";
			List<Map<String,Object>> listYwgms = pomrDao.getYwgms(uid);
			for (Map<String, Object> ywgms : listYwgms) {
				strYwgms += "," + ywgms.get("ywgms");
			}
			if(!"".equals(strYwgms)){
				strYwgms = strYwgms.substring(1,strYwgms.length());
			}
			mapTmp.put("ywgms", strYwgms);	
			
			//bls
			String strBls = "";
			List<Map<String,Object>> listBls = pomrDao.getBls(uid);
			for (Map<String, Object> bls : listBls) {
				strBls += ","+ bls.get("blmc");
			}
			if(!"".equals(strBls)){
				strBls = strBls.substring(1,strBls.length());
			}
			mapTmp.put("bls", strBls);	
			
			
			//cj
			String strCj = "";
			List<Map<String,Object>> listCj = pomrDao.getCj(uid);
			for (Map<String, Object> cj : listCj) {
				strCj += ","+ cj.get("mc");
			}
			if(!"".equals(strCj)){
				strCj = strCj.substring(1,strCj.length());
			}
			mapTmp.put("cj", strCj);
			
			//rllx
			String strRllx = "";
			List<Map<String,Object>> listRllx = pomrDao.getRllx(uid);
			for (Map<String, Object> rllx : listRllx) {
				strRllx += ","+ rllx.get("mc");
			}
			if(!"".equals(strRllx)){
				strRllx = strRllx.substring(1,strRllx.length());
			}
			mapTmp.put("rllx", strRllx);
			
			//cs
			String strCs = "";
			List<Map<String,Object>> listCs = pomrDao.getCs(uid);
			for (Map<String, Object> cs : listCs) {
				strCs += ","+ cs.get("mc");
			}
			if(!"".equals(strCs)){
				strCs = strCs.substring(1,strCs.length());
			}
			mapTmp.put("cs", strCs);
			
			//ys
			String strYs = "";
			List<Map<String,Object>> listYs = pomrDao.getYs(uid);
			for (Map<String, Object> ys : listYs) {
				strYs += ","+ ys.get("mc");
			}
			if(!"".equals(strYs)){
				strYs = strYs.substring(1,strYs.length());
			}
			mapTmp.put("ys", strYs);
			
			//jb
			String strJb = "";
			List<Map<String,Object>> listJb = pomrDao.getJb(uid);
			for (Map<String, Object> jb : listJb) {
				strJb += ","+ jb.get("mc")+":"+ObjectUtils.getNotNull(jb.get("qzsj"));
			}
			if(!"".equals(strJb)){
				strJb = strJb.substring(1,strJb.length());
			}
			mapTmp.put("jb", strJb);
			
			//ss手术
			String strSs = "";
			List<Map<String,Object>> listSs = pomrDao.getSs(uid);
			for (Map<String, Object> ss : listSs) {
				
				strSs += ","+ ss.get("mc")+":"+ObjectUtils.getNotNull(ss.get("qzsj"));
			}
			if(!"".equals(strSs)){
				strSs = strSs.substring(1,strSs.length());
			}
			mapTmp.put("ss", strSs);
			
			//ws外伤
			String strWs = "";
			List<Map<String,Object>> listWs = pomrDao.getWs(uid);
			for (Map<String, Object> ws : listWs) {
				strWs += ","+ ws.get("mc")+":"+ObjectUtils.getNotNull(ws.get("qzsj"));
			}
			if(!"".equals(strWs)){
				strWs = strWs.substring(1,strWs.length());
			}
			mapTmp.put("ws", strWs);
			
			//sx输血
			String strSx = "";
			List<Map<String,Object>> listSx = pomrDao.getSx(uid);
			for (Map<String, Object> sx : listSx) {
				strSx += ","+ sx.get("mc")+":"+ObjectUtils.getNotNull(sx.get("qzsj"));
			}
			if(!"".equals(strSx)){
				strSx = strSx.substring(1,strSx.length());
			}
			mapTmp.put("ws", strWs);
			
			listdata.add(mapTmp);
		}
		
		
		return listdata;
	}

	public void setOcode(String ocode){
		this.ocode=ocode;
	}
}

